//
//  GHTabBar.m
//  12-itcastweibo-1
//
//  Created by GHapple on 15-1-28.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "GHTabBar.h"
#import "UIView+GHFrame.h"

@interface GHTabBar ()
@property(nonatomic,weak)UIButton *plusBtn;
@end


@implementation GHTabBar

// 在init方法中添加加号按钮

- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setplusBtn];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        [self setplusBtn];

    }
    return self;
}
- (void)setplusBtn{
    // 创建一个加号按钮
    UIButton *plusBtn = [[UIButton alloc]init];
    // 设置图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    self.plusBtn = plusBtn;
    // 添加
    [self addSubview:plusBtn];
    

}

// 在layout方法中设置按钮的frame 准备添加加号按钮并且设置按钮的frame
- (void)layoutSubviews{
    [super layoutSubviews];
    
    int index = 0;
    for (UIView *child in self.subviews) {
        // 判断是否是tabbarbottom, 因为是私有的 ,所以要通过它的父类
        // 或者通过  并且不是加号按钮的时候设置frame
        if ([child isKindOfClass:[UIControl class] ]&&![child isKindOfClass:[UIButton class]]) {
            
            // 2.计算每个按钮的frame
            CGFloat childW = self.frame.size.width / 5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index * childW;
            CGFloat childY = 0;
            
            if (index == 2) {
                index ++;
                childX = index * childW;
            }
            child.frame = CGRectMake(childX, childY, childW, childH);
            index ++;
            
        }
        
        
    }
    // 设置加号的frame
    self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
    self.plusBtn.centerX = self.width *0.5;
    self.plusBtn.centerY = self.height *0.5;
}





@end
